using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESBSimpleSamples.ServiceClient.Generator.Scripts;
using ESBSimpleSamples.ServiceClient.Generator.Components;
using ESBSimpleSamples.ServiceClient.Generator.WSDL;

namespace ESBSimpleSamples.ServiceClient.Generator
{
    public partial class frmMain : Form
    {
        private WSDLMetadata _metadata;
        private List<ScriptBase> _scripts;

        public frmMain()
        {
            InitializeComponent();
            this.LoadTabPages();
        }

        private void LoadTabPages()
        {
            this.tabOutput.TabPages.Clear();
            foreach (ScriptBase script in this.Scripts)
            {
                this.tabOutput.TabPages.Add(new ScriptTabPage(script));
            }
        }

        private List<ScriptBase> Scripts
        {
            get
            {
                if (this._scripts == null)
                {
                    this._scripts = new List<ScriptBase>();
                    this._scripts.Add(new SchemaScript());
                    this._scripts.Add(new RequestEntityScript());
                    this._scripts.Add(new ResponseEntityScript());
                    this._scripts.Add(new ServiceRequestScript());
                }
                return this._scripts;
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                this.cboMethodName.Items.Clear();
                this._metadata = new WSDLMetadata();
                this._metadata.Source = this.txtServiceName.Text;
                foreach (WSDLMethod method in this._metadata.Methods)
                {
                    this.cboMethodName.Items.Add(method.Name);
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnGenerate_Click_1(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                if (this.cboMethodName.SelectedIndex > -1)
                {
                    string methodName = this.cboMethodName.SelectedItem.ToString();
                    foreach (WSDLMethod method in this._metadata.Methods)
                    {
                        if (method.Name == methodName)
                        {
                            this.Generate(method);
                            break;
                        }
                    }
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void Generate(WSDLMethod method)
        {
            foreach (ScriptTabPage scriptPage in this.tabOutput.TabPages)
            {
                scriptPage.PopulateFromWSDL(this._metadata, method);
            }
        }
    }
}